class VendorController < ApplicationController
  before_filter :user_authorize,:except =>[:register,:create]
  before_filter :sys_before_log
  #before_filter :privilege_filter
  
  def offerInformationForPrint
  end

  def password
  end

  def passwordForVendor
  end

  def register
  end

  def registerInformation
  end

  def registerInformationForPrint
  end

  def rejectReason
  end

  def vendorInformation
  end

  def vendorInformationForVendor
  end

  def vendorList
  end

  def vendorListForVendor
  end

  #获得供应商列表
  def getVendors
    pageBegin =  params[:start]
    pageEnd =  params[:limit]
    vendors = Vendor.findVendors(params[:vendor_number].to_s,params[:vendor_name].to_s,params[:state].to_s,pageBegin.to_s,pageEnd.to_s)
    jsonstr="{totalProperty:"+vendors.length.to_s+",root:"+vendors.to_json(:only => [:id,:name,:login_id,:company_name,:legal_person,:linkman,:linkman_phone,:linkman_mobile,:linkman_email,:auditing_status])+"}"
    #logger.info jsonstr
    render :text=>jsonstr, :layout=>false
  end

  def getMyInformation
    vendor = Vendor.find(session[:vendor_id])
    jsonstr="{totalProperty:1,root:"+vendor.to_json(:only => [:id,:name,:login_id,:company_name,:legal_person,:linkman,:linkman_phone,:linkman_mobile,:linkman_email,:auditing_status])+"}"
    #logger.info jsonstr
    render :text=>jsonstr, :layout=>false
  end

  #审批人，启用供应商
  def applyPass
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        vendor = Vendor.find(id)
        vendor.auditing_status = '启用'
        vendor.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #禁用供应商
  def stopVendor
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        vendor = Vendor.find(id)
        vendor.auditing_status = '禁用'
        vendor.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #供应商修改自己的密码
  def changePassword
    begin
      vendor = Vendor.find(session[:vendor_id])
      if vendor.password == params[:password]
        vendor.password = params[:password1]
        vendor.save!
        info = 'success'
      else
        info = '原密码不正确，请确认'
      end
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #获得供应商信息
  def edit
    if session[:auditor_id]!=nil || session[:applicant_id]!=nil
      vendor = Vendor.find(params[:id])
    else
      vendor = Vendor.find(session[:vendor_id])
    end    
    render :text => vendor.to_json(:except=>[:legal_person_photo,:business_license_photo,:aptitude_photo]),:layout=>false
  end

  #供应商更新供应商信息
  def update
    begin
      vendor = Vendor.find(params[:id])
      vendor.company_name = params[:company_name]
      vendor.company_address = params[:company_address]
      vendor.company_nature = params[:company_nature]
      vendor.registered_capital = params[:registered_capital]
      vendor.employee_number = params[:employee_number]
      vendor.turnover = params[:turnover]
      vendor.organization_code = params[:organization_code]
      vendor.business_license = params[:business_license]
      vendor.legal_person = params[:legal_person]
      vendor.legal_person_id_card = params[:legal_person_id_card]
      vendor.tax_number = params[:tax_number]
      vendor.deposit_bank = params[:deposit_bank]
      vendor.bank_account = params[:bank_account]
      vendor.industry = params[:industry]
      vendor.operation = params[:operation]
      vendor.introduction = params[:introduction]

      vendor.linkman = params[:linkman]
      vendor.linkman_position = params[:linkman_position]
      vendor.linkman_phone = params[:linkman_phone]
      vendor.linkman_mobile = params[:linkman_mobile]
      vendor.linkman_fax = params[:linkman_fax]
      vendor.linkman_email = params[:linkman_email]

      vendor.principal = params[:principal]
      vendor.principal_position = params[:principal_position]
      vendor.principal_phone = params[:principal_phone]
      vendor.principal_mobile = params[:principal_mobile]
      vendor.principal_fax = params[:principal_fax]
      vendor.principal_email = params[:principal_email]

      vendor.auditing_status = '未审批'

      if params[:business_license_photo]!=nil
        vendor.business_license_photo = params[:business_license_photo].read
      end

      if params[:legal_person_photo]!=nil
        vendor.legal_person_photo = params[:legal_person_photo].read
      end

      if params[:aptitude_photo]!=nil
        vendor.aptitude_photo = params[:aptitude_photo].read
      end
      
      vendor.save!
      info = 'success'
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    logger.info result
    render :text=>result, :layout=>false

  end

  #审批人驳回供应商
  def rejectVendor
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        vendor = Vendor.find(id)
        vendor.auditing_status = '驳回'
        vendor.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #得到营业执照照片
  def getBusinessLicensePhoto
    if session[:auditor_id]!=nil
      vendor = Vendor.find(params[:id])
    else
      vendor = Vendor.find(session[:vendor_id])
    end
    
    render :text => vendor.business_license_photo, :layout => false
  end

  #得到法人代表身份证照片
  def getLegalPersonPhoto
    vendor = Vendor.find(params[:id])
    render :text => vendor.legal_person_photo, :layout => false
  end

  #得到资质文件图片
  def getAptitudePhoto
    vendor = Vendor.find(params[:id])
    render :text => vendor.aptitude_photo,:layout => false
  end

  #供应商注册
  def create
    begin
      vendor = Vendor.findByLoginId(params[:login_id])
      if vendor!=nil
        info = '该用户名已使用，请更换'
      else
        vendor = Vendor.new
        vendor.login_id = params[:login_id]
        vendor.password = params[:password1]
        vendor.company_name = params[:company_name]
        vendor.company_address = params[:company_address]
        vendor.company_nature = params[:company_nature]
        vendor.registered_capital = params[:registered_capital]
        vendor.employee_number = params[:employee_number]
        vendor.turnover = params[:turnover]
        vendor.organization_code = params[:organization_code]
        vendor.business_license = params[:business_license]
        vendor.legal_person = params[:legal_person]
        vendor.legal_person_id_card = params[:legal_person_id_card]
        vendor.tax_number = params[:tax_number]
        vendor.deposit_bank = params[:deposit_bank]
        vendor.bank_account = params[:bank_account]
        vendor.industry = params[:industry]
        vendor.operation = params[:operation]
        vendor.introduction = params[:introduction]

        vendor.linkman = params[:linkman]
        vendor.linkman_position = params[:linkman_position]
        vendor.linkman_phone = params[:linkman_phone]
        vendor.linkman_mobile = params[:linkman_mobile]
        vendor.linkman_fax = params[:linkman_fax]
        vendor.linkman_email = params[:linkman_email]

        vendor.principal = params[:principal]
        vendor.principal_position = params[:principal_position]
        vendor.principal_phone = params[:principal_phone]
        vendor.principal_mobile = params[:principal_mobile]
        vendor.principal_fax = params[:principal_fax]
        vendor.principal_email = params[:principal_email]

        vendor.auditing_status = '未审批'

        if params[:business_license_photo]!=nil
          vendor.business_license_photo = params[:business_license_photo].read
        end

        if params[:legal_person_photo]!=nil
          vendor.legal_person_photo = params[:legal_person_photo].read
        end

        if params[:aptitude_photo]!=nil
          vendor.aptitude_photo = params[:aptitude_photo].read
        end

        vendor.save!
        info = 'success'
      end
      
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    logger.info result
    render :text=>result, :layout=>false
  end

  #得到注册信息
  def registerInformation
    @vendor = Vendor.find(session[:vendor_id])
    respond_to do |format|
      format.html 
    end
  end

  #得到注册信息
  def registerInformationForPrint
    @vendor = Vendor.find(session[:vendor_id])
    respond_to do |format|
      format.html
    end
  end

  #得到中标通知书信息
  #修改，供应商只能查看自己的中标信息。
  def offerInformationForPrint
    @purchase = Purchase.find(params[:id])
    respond_to do |format|
      format.html
    end
  end

  #审批人修改供应商的密码
  def changePasswordForVendor
    begin
      vendor = Vendor.find(params[:id])
      vendor.password = params[:password1]
      vendor.save!
      info = 'success'
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result,:layout=>false
  end
end
